﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace dzs.Web.supply
{
    public partial class ListTotalOverStock : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindDDL();
                Bind();

            }
        }


        private void BindDDL()
        {
            string str = " roles_id = 6 ";

            DataSet ds = new dzs.BLL.user().GetList(str);

            this.ddl_gygly.DataSource = ds;
            this.ddl_gygly.DataTextField = "user_name";
            this.ddl_gygly.DataValueField = "user_id";
            this.ddl_gygly.DataBind();

        }

        private void Bind()
        {
            Maticsoft.BLL.monthSupplayPlan msm = new Maticsoft.BLL.monthSupplayPlan();
            Maticsoft.BLL.supplyPlan sm = new Maticsoft.BLL.supplyPlan();

            List<dzs.Model.configinfo> cm = new dzs.BLL.configinfo().GetModelList("");//获取时间结算日期
            dzs.Model.configinfo c = cm[0];

            string user_id = this.ddl_gygly.SelectedItem.Value;
            string type = this.ddl_dhysd.SelectedItem.Value;

            string str = " user_id = " + user_id + " and (arrivalGoodsBillStatus = '' or arrivalGoodsBillStatus is null) ";

            if (type == "0")//全部
            {
                DateTime warnDate = DateTime.Now.AddDays(-15);//当前日期减去30天(一般到货验收单预警时间:15天、恶意到货验收单预警时间：30天)
                str += " and lastArrivalDate <='" + warnDate + "'   ";
            }
            if (type == "1")//一般到货验收单
            {
                str += " and lastArrivalDate <='" + DateTime.Now.AddDays(-15) + "' ";
            }
            if (type == "2")//恶意到货验收单
            {
                //str += " and lastArrivalDate between '" + DateTime.Now.AddDays(-30) + "' and '" + DateTime.Now.AddDays(-15) + "' ";
                str += " and lastArrivalDate <='" + DateTime.Now.AddDays(-30) + "'   ";
            }

            //最后一次到货日期不为null则判断为“到货后”,到货后预警日期内“到货通知单确认状态”为空或者为null时代表“到货后未办理到货验收单”
            DataSet ds_monthSupply = msm.GetListMonthSupplyTotalCount(str);

            this.lbl_complate.Text = "到货后规定时间内未办理到货验收单有"
                + ds_monthSupply.Tables[0].Rows.Count + "条";

            this.GridView1.DataSource = ds_monthSupply;
            this.GridView1.DataBind();


            this.lbl_supplyDateWarn.Text = "*一般到货验收单预警时间:15天、恶意到货验收单预警时间：30天";
        }

        protected void btn_sel_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;

            Bind();
        }

        //导出复写
        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }
        //导出excel
        protected void btn_out_Click(object sender, EventArgs e)
        {
            this.GridView1.AllowPaging = false;

            Bind();

            if (this.GridView1.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=yuegongjihua.xls");
                //Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>当月供应计划生成及时率</H4>");
                this.GridView1.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }

        }
    }
}